﻿using System;
using System.ComponentModel.Composition;
using Csla.Xaml;
using Microsoft.Practices.Prism.Regions;
using BizControl.Presentation.Infrastructure.Constants;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Input;
using System.Collections.Generic;
using BizControl.Library.BusinessObjects.Comments;
using BizControl.Presentation.Infrastructure.Controls;
using BizControl.Library.BusinessObjects.Activities;

namespace BizControl.Presentation.ActivityEditModule.ViewModels
{
  public class CommentsViewModel : ViewModel<ActivityComments>, IHeaderInfoProvider<string>
  {
    private readonly DelegateCommand _addCommentCommand;

    public CommentsViewModel(ActivityBO activity)
    {
      ManageObjectLifetime = false;
      _addCommentCommand = new DelegateCommand(AddComment);
      Activity = activity;
    }

    private void Refresh()
    {
      DoRefresh(GetComments);
    }

    protected override void OnModelChanged(ActivityComments oldValue, ActivityComments newValue)
    {
      base.OnModelChanged(oldValue, newValue);
      RefreshAllProperties();
    }

    private void RefreshAllProperties()
    {
    }

    private ActivityComments GetComments()
    {
      if (Activity != null)
        return Activity.Comments;
      return null;
    }

    #region Handlers

    private void AddComment()
    {
      Model.AddComment();
    }

    #endregion

    #region Properties

    public ICommand AddCommentCommand
    {
      get { return _addCommentCommand; }
    }

    private ActivityBO _activity;
    public ActivityBO Activity
    {
      get { return _activity; }
      set
      {
        if (_activity != value)
        {
          _activity = value;
          Refresh();
          OnPropertyChanged("Activity");
        }
      }
    }

    public Dictionary<string, int> Priorities
    {
      get
      {
        return BizControl.Presentation.Infrastructure.Helpers.Priorities.ObjectPrioritiesAsDictionary;
      }
    }

    #endregion

    #region IHeaderInfoProvider<string> Members

    public string HeaderInfo
    {
      get 
      {       
        return BizControl.Presentation.ActivityEditModule.Properties.Resources.CommentsHeaderInfo; 
      }
    }

    #endregion
  }
}
